﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;

using Ext.Net;
using BG.Data;
using BG.Service;
using Kwan.Data;

namespace BG.Web.Admin.Consume
{
    public partial class Default : UIPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                //BtnAddClassInfo.Hidden = !permit[0];
                //AppendChild.Hidden = !permit[0];
                //BtnAddInfo.Hidden = !permit[0];
                //Rename.Hidden = !permit[2];
                //btnChangeHot.Hidden = !permit[2];

                //BtnDeleteInfo.Hidden = !permit[3];
                //Remove.Hidden = !permit[3];
                //if (permit[3])
                //{
                //    List<GridCommand> gridCommand = new List<GridCommand>{
                //        ExtHelper.CreateGridCommand("Delete", Icon.Delete, "删除", "删除该条记录")
                //    };
                //    GridPanel1.ColumnModel.Columns.Add(ExtHelper.CreateCommandColumn(gridCommand));
                //}
                //ResourceManager1.RegisterIcon(Icon.Image);
                qStartDate.Text = DateTime.Now.ToShortDateString();
                qEndDate.Text = DateTime.Now.ToShortDateString();
            }
        }

        protected void BtnAddInfo_Click(object sender, EventArgs e)
        {
            SetEditWindow(new TConsume());
            EditWindow.Show();
        }

        [DirectMethod]
        public void DeleteSelected(string obj)
        {
            string[] list = obj.Split(',');
            (TConsume.entityID == list).Delete<TConsume>();
            Store1.DataBind();
        }

        [DirectMethod]
        public void ChangeState(string obj, string NameValue)
        {
            IList<TConsume> list = JSON.Deserialize<IList<TConsume>>(obj);
            if (list.Count > 0)
            {
                CrudManage.ChangeState(list, NameValue);
                Store1.DataBind();
            }
            else
            {
                ExtHelper.GetNotification("系统提示", "请至少选择一项！").Show();
            }
        }

        [DirectMethod]
        public void EditInfo(string EntityID)
        {
            if (!permit[2]) return;
            SetEditWindow((TConsume.entityID == int.Parse(EntityID)).ListFirst<TConsume>());
            EditWindow.Show();
        }

        protected void RefreshDS(object sender, StoreRefreshDataEventArgs e)
        {
            NameValueCollection ps = new NameValueCollection();
            ps.Set("gridsort", e.Sort);
            ps.Set("dir", e.Dir.ToString());

            //搜索项
            if (string.IsNullOrEmpty(txtFilter.Text))
                ps.Set("tag", string.Format(",{0},{1}", DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd")));
            else
                ps.Set("tag", txtFilter.Text);

            int pageindex = e.Start / e.Limit;
            Kwan.Data.Region r = new Kwan.Data.Region(pageindex, e.Limit);

            Expression exp = ConsumeQuery.MakeSearchWhere(ps);
            IList<VConsume> ListView = exp.List<VConsume>(r, new string[] { e.Sort + " " + e.Dir });

            e.Total = new Expression().Count<TConsume>();
            Store1.DataSource = ListView;
            Store1.DataBind();
        }

        protected void SetEditWindow(TConsume tt)
        {
            //txtID.Text = tt.EntityID.ToString();
            //txtEnable.Checked = tt.Enable;
            //txtHot.Checked = tt.Hot; ;
            //txtCreatedDate.Text = tt.CreatedDate.ToString();
            //txtItem.Text = tt.Item;
            //txtNett.Text = tt.Nett.ToString();
            //txtOrderID.Text = tt.OrderID.ToString();
            //txtPrice.Text = tt.Price.ToString();
            //txtProductID.Text = tt.ProductID.ToString();
            //txtReceivable.Text = tt.Receivable.ToString();
            //txtRoomNO.Text = tt.RoomNO;
            //txtStatus.Text = tt.Status.ToString();
            //txtTotal.Text = tt.Total.ToString();
            //txtType.Text = tt.Type.ToString();
            //txtUserID.Text = tt.UserID;
        }

        protected void SaveEditWindow(object sender, DirectEventArgs e)
        {
            //TConsume tt = Convert.ToInt32(txtID.Text) > 0 ? 
            //    (TConsume.entityID == Convert.ToInt32(txtID.Text)).ListFirst<TConsume>() : new TConsume();

            //tt.CreatedDate = DateTime.Now;
            //tt.Item = txtItem.Text;
            //tt.Nett = double.Parse(txtNett.Text);
            ////tt.OrderID = int.Parse(txtOrderID.Text);
            //tt.Price = double.Parse(txtPrice.Text);
            ////tt.ProductID = int.Parse(txtProductID.Text);
            ////tt.Receivable = double.Parse(txtReceivable.Text);
            ////tt.RoomNO = txtRoomNO.Text;
            //tt.Status = int.Parse(txtStatus.Text);
            //tt.Total = double.Parse(txtTotal.Text);
            //tt.Type = int.Parse(txtType.Text);
            ////tt.UserID = txtUserID.Text;
            ////tt.Enable = txtEnable.Checked;
            ////tt.Hot = txtHot.Checked;

            //DBContext.Save(tt);
            //Store1.DataBind();
            //Store1.Dispose();
            //EditWindow.Hide();
        }
    }
}